﻿/*
 Plugin for smalljs toolkit
 Copyright (c) 2013 Walter M. Soto Reyes
 http://www.smalljs.com or https://smalljs.codeplex.com
 License: https://smalljs.codeplex.com/license
*/
(function () {
    if (window.smalljs && window.smalljs.ui) {

        var fs = {
            settings: {
                items: [],
                track: '',
                main: '',
                width: 0,
                height: 0,
                currentStart: 0,
                current: 1,
                reset: false, 
                inProgress: false,
                intervalId: null,
                auto:false
            },
            setup: function (params) {
                ///	<summary>
                ///  Setup frameSlider
                ///	</summary>
                ///	<param name="params" type="object">
                ///	 {  
                ///      items:[],
                ///      track:'track id',
                ///      main:'main frame id',
                ///      width:'frame width',
                ///      height:'frame height'
                ///  }
                ///	</param>

                if (typeof (params.items) !== 'undefined') { fs.settings.items = params.items; }
                if (typeof (params.track) !== 'undefined') { fs.settings.track = params.track; }
                if (typeof (params.main) !== 'undefined') { fs.settings.main = params.main; }
                if (typeof (params.width) !== 'undefined') { fs.settings.width = params.width; }
                if (typeof (params.height) !== 'undefined') { fs.settings.height = params.height; }

                sj(this.settings.track).style(
                    'width:' + (fs.settings.width * fs.settings.items.length + 7) + 'px',
                    'position:absolute',
                    'left:0px');

                sj(fs.settings.main).style('position:relative', 'width:' + (fs.settings.width) + 'px',
                    'height:' + fs.settings.height + 'px',
                    'overflow:hidden');

                for (var i = 0; i < fs.settings.items.length; i++) {
                    sj(fs.settings.items[i]).style(
                            'width:' + fs.settings.width + 'px',
                            'height:' + fs.settings.height + 'px',
                            'float:left',
                            'position:relative',
                            'left:0px');
                }
                 

            },
            onMove:null,
            completed: function () {
                fs.settings.inProgress = false;
                if (typeof(fs.onMove)==='function') {
                    fs.onMove();
                }
            },
            reset: function () {
                fs.move(-((this.settings.items.length - 1) * this.settings.width), 0);
                fs.settings.currentStart = 0;
                fs.settings.current = 0;
            },
            move: function (start, end) {
                if (!fs.settings.inProgress) {
                    fs.settings.inProgress = true;
                    sj(fs.settings.track).animateSide({
                        start: start,
                        end: end,
                        side: 'left',
                        animation: smalljs.ui.animations.pullBack,
                        callback: this.completed
                    });
                }
            },
            stopAuto: function () {
                if (fs.settings.intervalId !== null) {
                    clearInterval(fs.settings.intervalId);
                    fs.settings.auto = false;
                }
            },
            auto: function (delay,callback) {
                if(typeof(delay) === 'undefined'){ delay = 6000;}
                var fn = function () {
                    if (!fs.settings.inProgress) {
                        fs.next();
                        if (typeof (callback) === 'function') { callback(); }
                    }
                };
                fs.settings.auto = true;
               fs.settings.intervalId = setInterval(fn,delay);
            },
            go: function (pos,callback) {
                if (!fs.settings.inProgress) {
                    if (pos != fs.settings.current) {
                        var end = fs.settings.currentStart - (fs.settings.width * (pos - fs.settings.current));
                        if (pos < fs.settings.current) {
                            fs.settings.currentStart = -(fs.settings.current * fs.settings.width) + fs.settings.width;
                            end = -(fs.settings.width * pos) + fs.settings.width;
                        }
                        this.settings.current = pos;
                        this.move(fs.settings.currentStart, end);
                        if (typeof (callback) === 'function') { callback(); }
                        this.settings.currentStart = end;
                    }
                }
            },
            back: function (callback) {
                if (!fs.settings.inProgress) { 
                    if (fs.settings.current ==1) {
                        this.go(fs.settings.items.length);
                    } else {
                        var end = -(fs.settings.currentStart + fs.settings.width);
                        if (end > 0) { end = -(end); } 
                        fs.move(fs.settings.currentStart, end);
                        fs.settings.currentStart = end;
                        fs.settings.current--;
                    }
                    if (typeof (callback) === 'function') { callback(); }
                }
            },
            next: function (callback) {
                if (!fs.settings.inProgress) {
                    if (fs.settings.current >= fs.settings.items.length) {
                        fs.settings.reset = true;
                    }
                    if (fs.settings.reset) {
                        fs.settings.reset = false;
                        fs.reset();
                    } else {
                        var end = -(fs.settings.width * fs.settings.current);
                        
                        fs.move(fs.settings.currentStart, end);
                        fs.settings.currentStart = end;
                    }
                    fs.settings.current++;
                    if (typeof (callback) === 'function') { callback(); }
                }
            }

        };
        smalljs.extend({
            frameSlider: fs
        });
    } else {
        console.error('smalljs is required (https://smalljs.codeplex.com/)');
        console.error('smalljs.ui is required');
    } 
})();